package com.lanou.dllo.vc.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.lanou.dllo.vc.R;

import com.lanou.dllo.vc.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/12/8.
 * implements SectionIndexer
 */
public class TelAdapter extends ArrayAdapter<UserEntity> implements SectionIndexer {
 private List<UserEntity> datas;
 private LayoutInflater layoutInflater;
 private int res;
    private Context context;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 1) {
                notifyDataSetChanged();
            }
            return false;
        }
    });

    public void addData(List<UserEntity> usernames) {

        datas = usernames;

        Log.i("lanoufffff999", datas + "sss" + datas.size());
        Message message = new Message();
        message.what = 1;

        handler.sendEmptyMessage(message.what);

    }

    //
    private static class ViewHolder {
        ImageView avatar;
        TextView unreadMsgView;
        TextView nameTextview;
        TextView tvHeader;
    }

    @Override
    public UserEntity getItem(int position) {
        return datas.get(position);
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    public TelAdapter(Context context, int resource) {
        super(context, resource);
        this.res = resource;

        Log.i("lanou999", res + "buju");


        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public Object[] getSections() {
        return null;
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        for (int i = 0; i < getCount(); i++) {
            String firstLetter = datas.get(i).getHeader();
            char firstChar = firstLetter.charAt(0);
            if (firstChar == sectionIndex) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
        return datas.get(position).getHeader().charAt(0);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(res, null);
            holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);
            holder.unreadMsgView = (TextView) convertView.findViewById(R.id.unread_msg_number);
            holder.nameTextview = (TextView) convertView.findViewById(R.id.name);
            holder.tvHeader = (TextView) convertView.findViewById(R.id.header);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        UserEntity user = datas.get(position);
        Log.d("TelAdapter", "user");
        if (user == null) {
            Log.d("ContactAdapter", position + "ddddd");
        }


        String username = user.getUsername();
        Log.d("lanou999", "--->用户名" + username);

        String header = username.substring(0, 1).toUpperCase();


        Log.d("lanou999", position + "--->头头" + header);

        if (username != null && username.length() > 0) {
            holder.nameTextview.setText(username);
        }

        int section = getSectionForPosition(position);
        if (position == getPositionForSection(section)) {
            // 第一次出现该section
            //   mHolder.textView_item_firstletter.setVisibility(View.VISIBLE);
            holder.tvHeader.setVisibility(View.VISIBLE);
            holder.tvHeader.setText(user.getHeader());
            // // mHolder.textView_item_firstletter.setText(userModel
//                    .getFirstLetter());
            holder.tvHeader.getPaint().setFlags(
                    Paint.UNDERLINE_TEXT_FLAG);
        } else {
            holder.tvHeader.setVisibility(View.GONE);
        }
        return convertView;
    }


}

